package com.ithong.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ithong.entity.Bills;
import com.ithong.service.BillService;
import com.ithong.utils.DataGridViewResult;
import com.ithong.vo.BillVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bill")
public class BillController {

    @Resource
    private BillService billService;

    @RequestMapping("/list")
    public DataGridViewResult list(BillVo billVo){
//        Map<String, Object> map = new HashMap<String, Object>();
        //设置分页信息（当前页码，每页显示数量）
        PageHelper.startPage(billVo.getPage(),billVo.getLimit());
        //调用分页查询账单列表的方法
        List<Bills> billList = billService.findBillList(billVo);
        //创建分页对象
        PageInfo<Bills> pageInfo = new PageInfo<Bills>(billList);
        //将数据表格返回的key保存到map集合中
//        map.put("code",0);
//        map.put("msg","");
//        map.put("count",pageInfo.getTotal());//总数量
//        map.put("data",pageInfo.getList());//数据列表
//        return JSON.toJSONString(map);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加
     * @param bills
     * @return
     */
    @RequestMapping("/addBill")
    public String addBill(Bills bills){
        Map<String, Object> map = new HashMap<String, Object>();
        if (billService.addBill(bills)>0){
            map.put("success",true);
            map.put("message","添加成功");
        }else {
            map.put("success",false);
            map.put("message","添加失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/updateBill")
    public String updateBill(Bills bills){
        Map<String, Object> map = new HashMap<String, Object>();
        if (billService.updateBill(bills)>0){
            map.put("success",true);
            map.put("message","修改成功");
        }else {
            map.put("success",false);
            map.put("message","修改失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        if (billService.deleteById(id)>0){
            map.put("success",true);
            map.put("message","删除成功");
        }else {
            map.put("success",false);
            map.put("message","删除失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/batchDelete")
    public String batchDelete(Integer[] ids){
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        for (int i = 0;i <ids.length;i++) {
            count = billService.deleteById(ids[i]);
            if (count > 0) {
                map.put("success", true);
                map.put("message", "删除成功");
            }
        }
        //判断受影响行数是否为0
        if (count<=0){
            map.put("success",false);
            map.put("message","删除失败");
        }

        return JSON.toJSONString(map);
    }
}
